// 步进器
interface IStepProps {
  min?: number;
  max: number;
  step: number;
  defQuantity: number;
  onChange?: any;
  curItem?: {}
}
interface IStepData {
  quantity: number;
}
interface IStepMethods {
  change?(cnt: number, type?: string): void;
  btnClick?(e): void;
  onBlur?(e): void;
  initData?(): void;
}
Component<IStepData, IStepProps, IStepMethods>({
  data: {
    quantity: 0
  },
  props: {
    step: 1,
    min: 0,
    max: 999,
    defQuantity: 0
  },
  onInit() {
    this.initData();
  },
  didUpdate(prvProps) {
    if (prvProps.defQuantity !== this.props.defQuantity) {
      this.initData();
    }
  },
  methods: {
    initData() {
      const { defQuantity } = this.props;
      if (defQuantity) {
        this.setData({
          quantity: defQuantity
        });
      }
    },
    btnClick({ currentTarget: { dataset } }) {
      const { key } = dataset;

      let cnt = this.data.quantity;

      if (key == "add") {
        cnt += this.props.step;
      } else {
        cnt -= this.props.step;
      }

      this.change(cnt, key);
    },

    onBlur(e) {
      this.change(e.detail.value);
    },

    change(cnt, type) {
      const { min, max } = this.props;

      cnt = Math.max(cnt, min);
      cnt = Math.min(cnt, max);
      this.setData({
        quantity: cnt
      });
      const parame = {
        cnt,
        type
      };
      this.props.onChange && this.props.onChange({...parame, item:this.props.curItem || {}});
    }
  }
});
